const http = require("http")
const fs = require("fs")
const mysql = require("mysql")

http.createServer((req, res) => {

  if (req.url == '/') {
    var fileContent = fs.readFileSync("./chapterList.html")
    res.writeHead(200, { "Content-Type": "text/html;charset=utf8" })
    res.write(fileContent)
    res.end()
  } else if (req.url == '/getChapterList') {
    var con = mysql.createConnection({
      host: '192.168.45.144',
      port: 3306,
      user: 'root',
      password: 'ddd',
      database: 'mydatabase'
    })
    var selectSql = "select * from list"
    con.query(selectSql, (err, request) => {
      if (err) {
        console.log(err)
      } else {
        var list = request;
      }
      console.log(list)
      res.writeHead(200, { "Content-Type": "text/html;charset=utf8" })
      res.write(JSON.stringify(list))
      res.end()
    })
    }else if(req.url == '/detail') {
      var content = fs.readFileSync("./chapter.html")
      res.writeHead(200, { "Content-Type": "text/html;charset=utf8" })
      res.write(content)
      res.end()
  }else if(req.url=="/getDetail"){
    var con = mysql.createConnection({
      host: '192.168.45.144',
      port: 3306,
      user: 'root',
      password: 'ddd',
      database: 'mydatabase'
    })
    var selectSql = "select * from list"
    con.query(selectSql, (err, request) => {
      if (err) {
        console.log(err)
      } else {
        var list = request;
      }
      console.log(list)
      res.writeHead(200, { "Content-Type": "text/html;charset=utf8" })
      res.write(JSON.stringify(list))
      res.end()
    })
  }
}).listen(8081)

